﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI.WebControls;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.Announcement
{
    public partial class View : BasePage_BackEnd
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            pager.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);
            if (!IsPostBack)
            {
                pager.CurrentPageIndex = 0;
                PagerControll_PageChange(pager.CurrentPageIndex);
            }
        }
       
        protected void PagerControll_PageChange(int pageIndex)
        {
            string language = Language.ToString();
            var dataPagerSource = AnnouncementPresent.GetPaged(pageIndex, pager.PageSize, GetLambaCondition(), p => p.CreateTime, true);
            AnnouncementGW.DataSource = dataPagerSource;
            AnnouncementGW.DataBind();
            pager.IntialProperties(dataPagerSource.TotalCount);
        }

        private Expression<Func<Model.Announcement, bool>> GetLambaCondition()
        {
            string language = string.Empty;
            if (chbCN.Checked)
                language = Language.CN.ToString();
            else if (chbEN.Checked)
                language = Language.EN.ToString();
            Expression<Func<Model.Announcement, bool>> searchExp = r => r.Language.Contains(language);
            if (txtFromDate.Text != null && txtFromDate.Text.Trim() != string.Empty)
            {
                var fromDate = Convert.ToDateTime(txtFromDate.Text);
                searchExp = searchExp.AndPredicate<Model.Announcement>(p => p.CreateTime >= fromDate);
            }
            if (txtToDate.Text != null && txtToDate.Text.Trim() != string.Empty)
            {
                var toDate = Convert.ToDateTime(txtToDate.Text);
                DateTime overtoDate = toDate.AddDays(1);
                searchExp = searchExp.AndPredicate<Model.Announcement>(p => p.CreateTime < overtoDate);
            }
            return searchExp;
        }

        public  override void InitControlResource()
        {
            btnAdd.Text = ManageResource.mgr_btnCommonAdd;
            btnQuery.Text = ManageResource.mgr_btnQuery;
            lblDate.Text = NameResource.Announcement_CreateTime;
            lblLanguage.Text = NameResource.Language;
            chbAll.Text = NameResource.Product_Status_All;
            chbCN.Text = NameResource.Language_CN_Label;
            chbEN.Text = NameResource.Language_EN_Label;
            revtxtFromDate.ErrorMessage = CommonResource.ErrorFormat;
            revtxtToDate.ErrorMessage = CommonResource.ErrorFormat;
            int i = 0;
            AnnouncementGW.Columns[i++].HeaderText = NameResource.No;
            AnnouncementGW.Columns[i++].HeaderText = NameResource.Announcement_Subject;
            AnnouncementGW.Columns[i++].HeaderText = NameResource.Announcement_CreateBy;
            AnnouncementGW.Columns[i++].HeaderText = NameResource.Announcement_CreateTime;
            AnnouncementGW.Columns[i++].HeaderText = ManageResource.mgr_lblAction;
        }

        protected void AnnouncementGW_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var lbtnDelete = e.Row.Cells[4].FindControl("lbtnDelete") as LinkButton;
                var lbtnUpdate = e.Row.Cells[4].FindControl("lbtnUpdate") as LinkButton;
                (e.Row.Cells[0].FindControl("lblNum") as Label).Text = (e.Row.RowIndex + 1 + (pager.CurrentPageIndex) * pager.PageSize).ToString();
                Model.Announcement announcement = e.Row.DataItem as Model.Announcement;
                if (announcement != null)
                {
                    var user = UserPresent.GetById(announcement.CreateBy);
                    if (user != null)
                        (e.Row.Cells[2].FindControl("lblCreateBy") as Label).Text = user.NameCN;
                    lbtnDelete.Text = ManageResource.mgr_btnDelete;
                    lbtnUpdate.Text = ManageResource.mgr_btnEdit;
                    lbtnDelete.Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                }
                if (IsLabManager & CurrentUserId != announcement.CreateBy)
                {
                    lbtnDelete.Attributes.Add("disabled", "disabled");
                    lbtnDelete.Attributes.Remove("onclick");
                    lbtnDelete.Attributes.Add("onclick", "return false;");
                    lbtnUpdate.Attributes.Add("disabled", "disabled");
                    lbtnUpdate.Attributes.Remove("onclick");
                    lbtnUpdate.Attributes.Add("onclick", "return false;");
                }
            }
        }

        protected void AnnouncementGW_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = e.RowIndex;
            string ID = this.AnnouncementGW.DataKeys[i]["Id"].ToString();
            Response.Redirect("/Announcement/Update.aspx?PageStatus=edit&id=" + ID);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Announcement/Update.aspx?PageStatus=new");
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            lblOverError.Text = string.Empty;
            if (!string.IsNullOrEmpty(txtFromDate.Text) && !string.IsNullOrEmpty(txtToDate.Text))
            {
                var fromDate = Convert.ToDateTime(txtFromDate.Text);
                var toDate = Convert.ToDateTime(txtToDate.Text);
                if (fromDate > toDate)
                {
                    lblOverError.Text = NameResource.OverTimeError + "！";
                    return;
                }
            }
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void AnnouncementGW_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = e.RowIndex;
            string ID = this.AnnouncementGW.DataKeys[i]["Id"].ToString();
            var AnnouncementId = Int32.Parse(ID);
            AnnouncementPresent.Delete(AnnouncementId);
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

    }
}